package com.begincode.common.http;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created by yangsj on 2016/4/27.
 * Http异步请求工具类
 */
public class HttpAsyncUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpAsyncUtil.class);
    private static CloseableHttpAsyncClient httpclient;

    static {
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(3000)
                .setConnectTimeout(3000).build();
        httpclient = HttpAsyncClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .build();
        httpclient.start();
    }

    public static void main(String[] args){
        Future<HttpResponse> responseFuture1 =  post("http://www.begincode.net");
        Future<HttpResponse> responseFuture2 =  post("http://www.begincode.net");
        System.out.println("异步响应");
        System.out.println(getResponseStr(responseFuture2));
        System.out.println(getResponseStr(responseFuture1));
    }

    public static Future<HttpResponse> post(String url) {
        HttpPost httpPost = new HttpPost("http://www.begincode.net/");
        Future<HttpResponse> httpResponse = httpclient.execute(httpPost, null);
        return httpResponse;
    }

    public static <T> T formatResponse(Future<HttpResponse> responseFuture,Class<T> T)  {

        try {
            String responseStr = getResponseStr(responseFuture);
            if(StringUtils.isNotBlank(responseStr)){
                System.out.println("格式化");
            }
            return T.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            logger.error("数据转换异常 InterruptedException", e);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            logger.error("数据转换异常 InterruptedException", e);
        }
        return null;
    }


    private static String getResponseStr(Future<HttpResponse> responseFuture) {
        try {
            HttpResponse response = responseFuture.get();
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println("格式化");
                return EntityUtils.toString(entity);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            logger.error("数据转换异常 InterruptedException", e);
        } catch (ExecutionException e) {
            e.printStackTrace();
            logger.error("数据转换异常 ExecutionException", e);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("数据转换异常 ExecutionException", e);
        }
        logger.info("无响应实体");
        return "";
    }
}
